import { createStore } from 'vuex'

// 引入封装好的模块
import menu from './modules/menu'
import role from  './modules/role/role'
import user from  './modules/user/user'


export default createStore({
    state:{
      userinfo:sessionStorage.getItem("userinfo") 
      ?JSON.parse(sessionStorage.getItem("userinfo") || "0")
      :null
    },
    getters:{
      getUser(state){
        return state.userinfo
      }
    },
    mutations:{
      REQ_USER(state,payload){
        if(payload){
          state.userinfo = payload;
          // 把payload存储到会话存储中
          sessionStorage.setItem("userinfo", JSON.stringify(payload));
        }else{
          sessionStorage.removeItem("userinfo")
        }
      }
    },
    actions:{
      changeUserAction({commit},payload){
        commit("REQ_USER",payload)
      }
    },
  modules: {
    menu,
    role,
    user
  }
})
